package inline.core;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.Menu.Item;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class InlineProductsActivity extends ListActivity {
	// private static final String LOG_TAG = "InlineProductsActivity";

	private ProductsDbAdapter mDbHelper;
	private InlineService inlineService;
	//private Cursor basketCursor;
	private Cursor productCursor;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.product_list);
	}

	@Override
	public void onResume() {
		super.onResume();
		inlineService = new InlineService(this);
		inlineService.open();
		mDbHelper = inlineService.getProductsDb();
		fillData();
	}
	
	@Override
	protected void onFreeze(Bundle outState) {
		super.onFreeze(outState);
			stopManagingCursor(productCursor);
			inlineService.close();
	}
	
	/* ------------------------- Menu --------------------------------- */
	

	private static final int BASKET_ID = Menu.FIRST;
	private static final int QUIT_ID = Menu.FIRST + 1;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, BASKET_ID, "Ver Carrinho");
		menu.add(1, QUIT_ID, "Sair");
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, Item item) {
		switch (item.getId()) {

		case BASKET_ID:
			int requestCode = InlineBasketActivity.SHOW_PRODUCTS_CODE;
			Intent intent = new Intent(this,InlineBasketActivity.class);
			startSubActivity(intent, requestCode);
			return true;
		case QUIT_ID:
			finish();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);

	}

	private void fillData() {
		// Get all of the rows from the database and create the item list
		Bundle extras = this.getIntent().getExtras();
		int marketId = (Integer) extras.get(MarketDbAdapter.KEY_ROWID);
		productCursor = mDbHelper.fetchAllProducts(marketId);
		startManagingCursor(productCursor);

		// Create an array to specify the fields we want to display in the
		// list (only TITLE)
		String[] from = new String[] { ProductsDbAdapter.KEY_NAME,
				ProductsDbAdapter.KEY_DESCRIPTION,
				ProductsDbAdapter.KEY_UNIT_PRICE };

		// and an array of the fields we want to bind those fields to (in
		// this case just text1)
		int[] to = new int[] { R.id.product_name, R.id.product_description,
				R.id.product_unit_price };

		// Now create a simple cursor adapter and set it to display
		SimpleCursorAdapter products = new SimpleCursorAdapter(this,
				R.layout.product_row, productCursor, from, to);
		setListAdapter(products);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Cursor c = basketCursor;
		// c.moveTo(position);
		Cursor product = inlineService.getProductsDb().fetchProduct(id);
		String productName = product.getString(product.getColumnIndex(ProductsDbAdapter.KEY_NAME));
		
		Toast.makeText(this, "Adicionando " + productName
				+ " ao carrinho de compras.", Toast.LENGTH_SHORT).show();
		
		 int amount = 1;
		 inlineService.getBasketDb().addToBasket((int)id,amount);
	}

}